﻿CloudDisk.AbstractView = function(cfg){
    this.el = null;
	this.itemTpl = cfg.itemTpl;
	this.eventListeners = [];
};

CloudDisk.AbstractView.prototype = {

    itemSelector: '',

    //simple method to fire event
	fireEvent: function(eventType, evt){
	    var listeners = this.eventListeners;
        if(listeners[eventType]) {
			var events = listeners[eventType];
			for(var i = 0; i < events.length; i++) {
				events[i].apply(this, Array.prototype.slice.call(arguments).slice(1));
			}
		}	
	},

    //simple method to register event handler
    on: function(eventType, handler) {
	    var listeners = this.eventListeners;
		if(!listeners[eventType]) {
			listeners[eventType] = [];
		}

		listeners[eventType].push(handler);
    },
	
    hide: function(){
	    this.el.hide();
	},
	
	show: function(){
	    this.el.show();
	},
	
	render: function(data){
	    var that = this;
		that.el.empty();
	    $.each(data, function(n, v){
		    that.el.append(that.createItem(v));
		});
		
		//show contextmenu when right clicked

		that.el.find(that.itemSelector).mousedown(function(e){
		    if (e.button == 2) {
			    e.preventDefault();
			    CloudDisk.FileContextMenu.show(this, {
				    x: e.pageX,
					y: e.pageY
				});          
		    }
		}).bind("contextmenu",function(e){
		    return false;
		});
	},
	
	createBlankItem: function(){	  
    
	},
	
	createItem: function(data){	  
    
	},
	
	onItemClick: function(){
	    var that = $(this);
		if(that.hasClass('selected')){
			that.removeClass('selected');
		} else {
			that.addClass('selected');
		}
	},
	
	addItem: function(data){
	    this.createItem(data).prepend(this.el);
	},
	
	removeItem: function(itemId){
	    
	}
};